import React, { Component } from 'react';

import { connect } from 'dva';

import moment from 'moment';

import { Row, Col, Card, Form, Input, Select, Icon, Button, Dropdown, Menu, InputNumber, DatePicker, Modal, message, Badge, Divider, Radio } from 'antd';

import MyIcon from '@/components/MyIcon';
import MyComponent from '@/components/MyComponent';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import StandardTable from '@/components/StandardTable';

import { recoveries as tabList } from '@/commons/tabList';
import { usersApply as columns } from '@/commons/columns';
import { usersApply as map } from '@/commons/map';

const FormItem = Form.Item;
const { TextArea } = Input;
const getValue = obj =>
  Object.keys(obj)
  .map(key => obj[key])
  .join(',');

@connect(({ apply, recoveries, loading }) => ({
  apply,
  recoveries,
  loading: loading.models.recoveries,
}))
export default class extends MyComponent {

  constructor(props) {
    super(props)
    this.namespaces = 'aaa'
  }

  state = {
    modalVisible: false,
    expandForm: false,
    selectedRows: [],
    formValues: {},
    queryRow: {},
  };

  columns = [...columns, {
    title: '操作',
    width: 99,
    align: 'center',
    fixed: 'right',
    render: (queryRow = {}) => (
      <Button 
        icon="edit" 
        type="primary" 
        size="small"
        ghost={queryRow.status}
        onClick={this.handleModalVisible.bind(null, queryRow)}
      >
        {queryRow.status ? '已审阅' : '待审阅'}
      </Button>
    )
  }];

  componentDidMount() {
    const { dispatch } = this.props;

    dispatch({
      type: 'apply/fetch',
    });
  }

  handleToggleTable = e => {
    e.preventDefault();

    const { form, dispatch } = this.props;
    form.resetFields();

    dispatch({
      type: 'apply/fetch',
      payload: { status: e.target.value }
    });
  }

  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
    this.setState({
      formValues: {},
    });
    dispatch({
      type: 'apply/fetch'
    });
  };

  toggleForm = () => {
    const { expandForm } = this.state;
    this.setState({
      expandForm: !expandForm,
    });
  };

  handleMenuClick = e => {
    const { dispatch } = this.props;
    const { selectedRows } = this.state;

    if (!selectedRows) return;
    switch (e.key) {
      case 'remove':
        dispatch({
          type: 'apply/remove',
          payload: { id: selectedRows.map(row => row.id) },
          callback: ({ errno, data }) => {
            if (!errno && data) {
              dispatch({
                type: 'apply/fetch'
              });
              this.setState({
                selectedRows: []
              });

              message.success(`共有${data}条申请已删除成功~`);
            } else {
              message.error('删除申请失败！');
            }
          },
        });
        break;
      case 'update':
        dispatch({
          type: 'apply/update',
          payload: { id: selectedRows.map(row => row.id) },
          callback: ({ errno, data }) => {
            if (errno) {
              message.error('已审阅失败！');
            } else {
              dispatch({
                type: 'apply/fetch'
              });
              this.setState({
                selectedRows: []
              });

              message.success(`共有${data}条申请已审阅成功~`);
            }
          },
        });
        break;
      default:
        break;
    }
  };

  handleSelectRows = rows => {
    this.setState(state => ({
      ...state,
      selectedRows: rows,
    }));
  };

  handleSearch = e => {
    e.preventDefault();

    const { dispatch, form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;

      const values = {
        ...fieldsValue,
        createdAt: fieldsValue.createdAt && fieldsValue.createdAt.format('YYYY-MM-DD'),
        updatedAt: fieldsValue.updatedAt && fieldsValue.updatedAt.format('YYYY-MM-DD'),
      };

      this.setState({
        formValues: values,
      });

      dispatch({
        type: 'apply/fetch',
        payload: values,
      });
    });
  };

  render() {
    const { apply, loading, form } = this.props;
    const { expandForm, selectedRows } = this.state;

    return (
      <PageHeaderWrapper 
        title="回收站" 
        action={moment().format('LLLL')}
        tabActiveKey="base"
        tabList={tabList}
        onTabChange={this.handleTabChange}
      >
      	<Card bordered={false} bodyStyle={{padding: 0}}>
          <StandardTable
            selectedRows={selectedRows}
            loading={loading}
            data={[]}
            scroll={{ x: 1500 }}
            columns={this.columns}
            expandedRowRender={record => <p style={{ marginBottom: 0 }}>备注：{record.remark}</p>}
            onSelectRow={this.handleSelectRows}
            onChange={this.handleStandardTableChange}
          />
        </Card>
      </PageHeaderWrapper>
    )
  }
}